/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.cicp;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.sinodata.bsm.cicp.message.Messenger;
import com.sinodata.bsm.cicp.util.SystemContext;
import com.sinodata.bsm.common.message.BsmActiveMQFactory;
import com.sinodata.bsm.common.utils.PropertiesUtil;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author tangli
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-5-8 下午5:03:48          tangli_ITSM        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public final class ProbeEnv {
    private final static Logger logger = Logger.getLogger(ProbeEnv.class);
    public static Long probeId = 1L;
    public static String probeName = "";
    public static String probeType = "";
    public static String probeIp = "localhost";

    public static void initConfig() throws Exception {
        logger.info("probe init...");
        Properties prop = new Properties();
        InputStreamReader isr = new InputStreamReader(new FileInputStream("cicp-conf/probe-cicp.properties"), "GBK");
        prop.load(isr);
        isr.close();
        probeId = Long.parseLong(prop.getProperty("probe.id"));
        probeName = prop.getProperty("probe.name");
        probeType = prop.getProperty("probe.type");
        probeIp = InetAddress.getLocalHost().getHostName();
    }

    public static void initEnv() throws Exception {
        PropertyConfigurator.configure("cicp-conf/log4j.properties");
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("cicp-conf/dbdrivers.conf"));
            String readLine = null;
            while ((readLine = reader.readLine()) != null) {
                readLine = readLine.trim();
                if (readLine.length() > 0 && !readLine.startsWith("#")) {
                    String className = readLine;
                    Class.forName(className);
                }
            }
        } catch (Exception e) {
            logger.error("load db drivers error", e);
        } finally {
            reader.close();
        }

        Properties prop = new Properties();
        InputStreamReader isr = new InputStreamReader(new FileInputStream("cicp-conf/probe-cicp.properties"), "GBK");
        prop.load(isr);
        isr.close();
        BsmActiveMQFactory.getInstance().init(prop);
        Messenger.getInstance().init(ProbeEnv.probeId);

        //quartz
        Properties quartz = new Properties();
        InputStreamReader tsr = new InputStreamReader(new FileInputStream("cicp-conf/quartz.properties"), "GBK");
        quartz.load(tsr);
        tsr.close();
        SystemContext.setProperty("quatrz.thread", quartz.getProperty("org.quartz.threadPool.threadCount"));

        //trap
        Properties trap = new Properties();
        InputStreamReader istrap = new InputStreamReader(new FileInputStream("cicp-conf/trap-cicp.properties"), "GBK");
        trap.load(istrap);
        istrap.close();

        SystemContext.setProperty("trap.open", trap.getProperty("trap.open", "false"));
        SystemContext.setProperty("trap.adress", trap.getProperty("trap.listen", "udp:localhost/162"));

    }

    public static void main(String[] args) throws Exception {
        Properties prop = null;
        try {
            prop = PropertiesUtil.getProperties("cicp-conf/probe-cicp.properties");
        } catch (Exception e) {
            e.printStackTrace();
        }
        String value = prop.getProperty("probe.name");
        System.out.println(value);
    }
}
